package br.ucb.cesbdao.visao;

import java.awt.Frame;
import java.text.ParseException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.servicos.Servicos;
import br.ucb.cesbdao.servicos.TimeUtil;
import br.ucb.cesbdao.servicos.Validadores;

/**
 *
 * @author Belgrano
 */
@SuppressWarnings("serial")
public class HoraDialog extends javax.swing.JDialog {
	static Logger logger = Servicos.carregaLog(HoraDialog.class);

	public HoraDialog() {
		super(new Frame(), true);
		initComponents();
	}
	
	/** Creates new form HoraDialog */
	public HoraDialog(java.awt.Frame parent, boolean modal){
		super(parent, modal);
		initComponents();
	}
//	public HoraDialog(){
//		initComponents();
//	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jbConfirmacao = new javax.swing.JButton();
		jpHora = new javax.swing.JPanel();
		jtHora = new javax.swing.JFormattedTextField();
		jlMsg = new javax.swing.JLabel();
		erroHora = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
		setLocationRelativeTo(null);
		setResizable(false);
		setTitle("Horário");
		erroHora.setFont(new java.awt.Font("Comic Sans MS", 1, 10));

		erroHora.setForeground(new java.awt.Color(255, 0, 0));
		
			try {
				jtHora.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
			} catch (ParseException e) {
				logger.error(e.getMessage());
				e.printStackTrace();
			}
	
		jbConfirmacao.setText("OK");
		jbConfirmacao.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jbConfirmacaoActionPerformed(evt);
			}
		});

		jpHora.setBorder(javax.swing.BorderFactory.createEtchedBorder());
		jbHoraAtual = new javax.swing.JButton();
		
				jbHoraAtual.setText("Hora Atual");
				jbHoraAtual.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jbHoraAtualActionPerformed(evt);
					}
				});

		javax.swing.GroupLayout jpHoraLayout = new javax.swing.GroupLayout(jpHora);
		jpHoraLayout.setHorizontalGroup(
			jpHoraLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(jpHoraLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(jtHora, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(jbHoraAtual)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(erroHora)
					.addContainerGap(139, Short.MAX_VALUE))
		);
		jpHoraLayout.setVerticalGroup(
			jpHoraLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(jpHoraLayout.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(jpHoraLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(jtHora, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(jbHoraAtual))
					.addContainerGap())
				.addGroup(Alignment.LEADING, jpHoraLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(erroHora)
					.addContainerGap(20, Short.MAX_VALUE))
		);
        jpHora.setLayout(jpHoraLayout);

		jlMsg.setFont(new java.awt.Font("Times New Roman", 2, 18)); // NOI18N
		jlMsg.setText("Informe a hora");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
							.addGap(78)
							.addComponent(jbConfirmacao))
						.addGroup(layout.createSequentialGroup()
							.addContainerGap()
							.addComponent(jlMsg, GroupLayout.PREFERRED_SIZE, 270, GroupLayout.PREFERRED_SIZE))
						.addGroup(layout.createSequentialGroup()
							.addContainerGap()
							.addComponent(jpHora, GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)))
					.addContainerGap())
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.TRAILING)
				.addGroup(layout.createSequentialGroup()
					.addGap(25)
					.addComponent(jlMsg, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(jpHora, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
					.addComponent(jbConfirmacao)
					.addGap(35))
		);
		getContentPane().setLayout(layout);

		pack();
	}// </editor-fold>

	private void jbConfirmacaoActionPerformed(java.awt.event.ActionEvent evt) {
		if(jtHora.getText().replace(":","").charAt(0) == ' '){
			setHora(jtHora.getText());
			dispose();
			
		}

		if(Validadores.validaHora("Hora invalida", jtHora, erroHora)){
			setHora(jtHora.getText());
			dispose();
		}


	}

	private void jbHoraAtualActionPerformed(java.awt.event.ActionEvent evt) {
		jtHora.setText(TimeUtil.getHoraSys());
	}
	// Variables declaration - do not modify
	private javax.swing.JFormattedTextField jtHora;
	private javax.swing.JButton jbHoraAtual;
	private javax.swing.JButton jbConfirmacao;
	private javax.swing.JLabel jlMsg;
	private javax.swing.JPanel jpHora;
	private static String hora;
	private javax.swing.JLabel erroHora;
	// End of variables declaration
	public static String getHora() {
		return hora;
	}
	@SuppressWarnings("static-access")
	public void setHora(String hora) {
		this.hora = hora;
	}




}